package com.xu.system.controller;

import com.xu.base.enums.ResponseCodeEnum;
import com.xu.base.utils.AssertUtils;
import com.xu.base.utils.LocalDateUtils;
import com.xu.system.model.pojo.workTime.AddWorkTimeConfigReq;
import com.xu.system.model.pojo.workTime.DelWorkTimeReq;
import com.xu.system.model.pojo.workTime.EditWorkTimeConfigReq;
import com.xu.system.model.pojo.workTime.SaveWorkTimeReq;
import com.xu.system.service.IWorkTimeService;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

/**
 * 工作日配置控制器
 *
 * @Author: 许乾伟
 * @Date: 2024/12/24 16:09
 */
@RestController
@RequestMapping("workTimeConfig")
public class WorkTimeConfigController {

    @Resource
    IWorkTimeService workTimeService;

    /**
     * 新增工作日配置
     * @param req
     * @return
     */
    @PostMapping("addConfig")
    public Boolean addConfig(@Valid @RequestBody AddWorkTimeConfigReq req) {
        verifyConfig(req);
        return workTimeService.addConfig(req);
    }

    /**
     * 更新工作日配置
     * @param req
     * @return
     */
    @PostMapping("editConfig")
    public Boolean editConfig(@Valid @RequestBody EditWorkTimeConfigReq req) {
        verifyConfig(req);
        return workTimeService.editConfig(req);
    }

    /**
     * 校验工作日配置
     * @param req
     */
    private void verifyConfig(AddWorkTimeConfigReq req){
        if  (req.getIsWorkTime()){
            AssertUtils.isNull(req.getWorkStartTime(), ResponseCodeEnum.PARAM_ERROR,"上班开始时间不能为空");
            AssertUtils.isNull(req.getWorkEndTime(), ResponseCodeEnum.PARAM_ERROR,"上班结束时间不能为空");
        }
        if (req.getIsNoonBreak()){
            AssertUtils.isNull(req.getNoonBreakStartTime(), ResponseCodeEnum.PARAM_ERROR,"午休开始时间不能为空");
            AssertUtils.isNull(req.getNoonBreakEndTime(), ResponseCodeEnum.PARAM_ERROR,"午休结束时间不能为空");
        }
    }
    /**
     * 保存日期配置
     * @param req
     * @return
     */
    @PostMapping("save")
    public Boolean save(@Valid @RequestBody SaveWorkTimeReq req) {
        return workTimeService.save(req);
    }
    /**
     * 删除日期配置
     * @param req
     * @return
     */
    @PostMapping("del")
    public Boolean del(@Valid @RequestBody DelWorkTimeReq req) {
        return workTimeService.del(req);
    }

    /**
     * 计算工作时间
     * @param startTime
     * @param endTime
     * @return
     */
    @GetMapping("getWorkTime")
    public Long getWorkTime(@RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime,@RequestParam("configCode") String configCode) {
        return workTimeService.getWorkTime(LocalDateUtils.yyyymmddhhmmssToLocalDateTime(startTime), LocalDateUtils.yyyymmddhhmmssToLocalDateTime(endTime),configCode);
    }

}
